package com.tang.dao.impl;

import com.tang.bean.Book;
import com.tang.bean.User;
import com.tang.dao.BaseDao;
import com.tang.dao.BookDao;

import java.sql.SQLException;
import java.util.List;

public class BookDaoImpl extends BaseDao<Book> implements BookDao {

  @Override
  public List<Book> selectBookList() throws SQLException {
    String sql = "select book_id bookId,book_name bookName,author,price,sales,stock,img_path imgPath,t_id typeId,type_name typeName from t_book";
    return getBeanList(Book.class,sql);
  }


  @Override
  public void deleteBook(Integer bookId) throws SQLException {
    String sql = "delete from t_book where book_id=?";
    update(sql,bookId);
  }


  @Override
  public void insertBook(Book book) throws SQLException {
    String sql = "insert into t_book (book_name,author,price,sales,stock,img_path,type_name) values (?,?,?,?,?,?,?)";
    update(sql,book.getBookName(),book.getAuthor(),book.getPrice(),book.getSales(),book.getStock(),book.getImgPath(),book.getTypeName());
    //更新t_id
    String updateSql="update t_book,t_booktype set t_book.t_id = t_booktype.t_id where t_book.type_name = t_booktype.t_name";
    update(updateSql);
  }


  @Override
  public Book selectBookByPrimaryKey(Integer bookId) throws SQLException {
    String sql = "select book_id bookId,book_name bookName,author,price,sales,stock,img_path imgPath,t_id typeId,type_name typeName from t_book where book_id=?";
    return getBean(Book.class,sql,bookId);
  }

  @Override
  public void updateBook(Book book) throws SQLException {
    //我们暂时不修改图片路径
    String sql = "update t_book set book_name=?,price=?,author=?,sales=?,stock=?,type_name=? where book_id=?";
    update(sql,book.getBookName(),book.getPrice(),book.getAuthor(),book.getSales(),book.getStock(),book.getTypeName(),book.getBookId());
    String updateSql="update t_book,t_booktype set t_book.t_id = t_booktype.t_id where t_book.type_name = t_booktype.t_name";
    update(updateSql);
  }

  @Override
  public void updateBookArr(Object[][] bookArrParam) {
    String sql = "update t_book set sales=sales+?,stock=stock-? where book_id=?";
    batchUpdate(sql,bookArrParam);
  }
  @Override
  public  Integer getBookCount() throws SQLException{
    String sql="select count(*) from t_book";
    Integer  count=(int)getBean(sql);
    return count;
  }

  @Override
  public List<Book> getPageItems(Integer pageBegin,Integer pageSize) throws SQLException {
    String sql="select book_id bookId,book_name bookName,author,price,sales,stock,img_path imgPath,t_id typeId,type_name typeName from t_book limit ?,?";
    return getBeanList(Book.class,sql,pageBegin,pageSize);
  }

  @Override
  public Integer getBookCountByPrice(Integer min, Integer max) throws Exception {
    String sql="select count(*) from t_book where price between ? and ?";
    Integer  count=(int)getBean(sql,min,max);
    return count;
  }

  @Override
  public List<Book> getPageItemsByPrice(Integer pageBegin, Integer pageSize, Integer min, Integer max) throws Exception {
    String sql="select book_id bookId,book_name bookName,author,price,sales,stock,img_path imgPath,t_id typeId,type_name typeName from t_book where price between ? and ? limit ?,?";
    return getBeanList(Book.class,sql,min,max,pageBegin,pageSize);
  }

  @Override
  public Integer getBookCountByType(String typeId) {
    typeId=typeId+'%';
    String sql="select count(*) from t_book where  t_id like ? ";
    Integer  count=(int)getBean(sql,typeId);
    return count;
  }

  @Override
  public List<Book> getPageItemsByType(Integer pageBegin, Integer maxPageSize, String typeId) {
    typeId=typeId+'%';
    String sql="select book_id bookId,book_name bookName,author,price,sales,stock,img_path imgPath,t_id typeId,type_name typeName from t_book where t_id like ? limit ?,?";
    //where left(t_id,3)= ?
    return getBeanList(Book.class,sql,typeId,pageBegin,maxPageSize);
  }

  @Override
  public Integer getBookCountByKey(String key) {
    key='%'+key+'%';
    String sql="select count(*) from t_book where book_name like ? OR author like ? ";
    Integer  count=(int)getBean(sql,key,key);
    return count;
  }

  @Override
  public List<Book> getPageItemsByKey(Integer pageBegin, Integer maxPageSize, String key) {
    key='%'+key+'%';
    String sql="select book_id bookId,book_name bookName,author,price,sales,stock,img_path imgPath,t_id typeId,type_name typeName from t_book where book_name like ? OR author like ? limit ?,?";
    return getBeanList(Book.class,sql,key,key,pageBegin,maxPageSize);
  }

}
